<?php

$db = new MyDB();

if(!$db) { 
		echo $db->lastErrorMsg();
}

// dla liczenia tygodniowego start tygodnia koniec
$custom_date = strtotime( date('d-m-Y', strtotime("now")) ); 
$week_start = date('Y-m-d', strtotime('this week last sunday', $custom_date));
$week_end = date('Y-m-d', strtotime('this week next sunday', $custom_date));

$count_sql = "select count(id) as count from access_".$_SESSION["uid"];

$results = $db->query($count_sql);
	while ($row = $results->fetchArray(SQLITE3_ASSOC)) {
		$count = $row['count'];
	}

$date = date('Y-m-d');

$sql_data = array();
$sql_data_weekly = array();
$interval_daily = array();
$interval_weekly = array();
$interval_break = array();

$format = 'Y-m-d H:i:s';
	
for($i=1;$i<$count+1;$i++) {
	$x = $i+1;
	$sql_access = "select date as start,(select date from access_".$_SESSION["uid"]." where id = ".$x.") as end from access_".$_SESSION["uid"]." where id = ".$i." and date like '".$date."%'";
	$sql_weekly = "select date as start_w,(select date from access_".$_SESSION["uid"]." where id = ".$x.") as end_w from access_".$_SESSION["uid"]." where id = ".$i." and date between '".$week_start."' and '".$week_end."'";
	//echo $sql_access."<br>";
	//echo $sql_weekly."<br>";
	$results_w = $db->query($sql_weekly);					// tygodniowo
	while ($row_w = $results_w->fetchArray(SQLITE3_ASSOC)) {
		if(empty($row_w['end_w'])) {
			$date = date('Y-m-d H:i:s');
			$row_w['end_w'] = $date;
		}
		array_push($sql_data_weekly, $row_w);
	}
	
	$results = $db->query($sql_access);
	while ($row = $results->fetchArray(SQLITE3_ASSOC)) {
		if(empty($row['end'])) {
			$date = date('Y-m-d H:i:s');
			$row['end'] = $date;
		}
		array_push($sql_data, $row);
	}
	$i++;
}

// liczymy przerwy
for($i=0;$i<sizeof($sql_data)-1;$i++) {
	$d_start = DateTime::createFromFormat($format, $sql_data[$i]["end"]);
	$d_end = DateTime::createFromFormat($format, $sql_data[$i+1]["start"]);
	//echo " START: " . $d_start->format('Y-m-d H:i:s');
	//echo " END: " . $d_end->format('Y-m-d H:i:s')."<br>";
	array_push($interval_break, $interval = $d_start->diff($d_end));
}

// liczymy dziennie przepracowany czas
for($i=0;$i<sizeof($sql_data);$i++) {
	$d_start = DateTime::createFromFormat($format, $sql_data[$i]["start"]);
	$d_end = DateTime::createFromFormat($format, $sql_data[$i]["end"]);
	//echo " START: " . $d_start->format('Y-m-d H:i:s');
	//echo " END: " . $d_end->format('Y-m-d H:i:s')."<br>";
	array_push($interval_daily, $interval = $d_start->diff($d_end));
}

// liczymy tygodniowo przepracowany czas
for($i=0;$i<sizeof($sql_data_weekly);$i++) {
	$d_start = DateTime::createFromFormat($format, $sql_data_weekly[$i]["start_w"]);
	$d_end = DateTime::createFromFormat($format, $sql_data_weekly[$i]["end_w"]);
	//echo " START: " . $d_start->format('Y-m-d H:i:s');
	//echo " END: " . $d_end->format('Y-m-d H:i:s')."<br>";
	array_push($interval_weekly, $interval = $d_start->diff($d_end));
}

$daily_new = new DateTime('00:00');
$daily_clone = clone $daily_new;

$break_new = new DateTime('00:00');
$break_clone = clone $break_new;

$weekly_new = new DateTime('00:00');
$weekly_clone = clone $weekly_new;

// weekly
for($i=0;$i<sizeof($interval_weekly);$i++) {
	$weekly_new->add($interval_weekly[$i]);
}

// daily
for($i=0;$i<sizeof($interval_daily);$i++) {
	$daily_new->add($interval_daily[$i]);
}

// breaks
for($i=0;$i<sizeof($interval_break);$i++) {
	$break_new->add($interval_break[$i]);
}

$weekly = $weekly_clone->diff($weekly_new)->format("%Hh : %Im");
$daily = $daily_clone->diff($daily_new)->format("%Hh : %Im");
$break = $break_clone->diff($break_new)->format("%Hh : %Im");

//echo "Total interval : ", $weekly, "\n";

$db->close();

?>